import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from '@angular/router';
import {Productdetail} from '../productdetail/productdetail.component';
import {Observable} from 'rxjs/index';
import {Injectable} from '@angular/core';
import {Product, ProductService} from '../share/product.service';


@Injectable()
export class ProductdetailResolve implements Resolve<Productdetail> {

  private product: Product;

  constructor(private router: Router, private productService: ProductService ) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Productdetail> | Promise<Productdetail> | Productdetail {

    const productId: number = route.params['id'];


    this.product = this.productService.getAllProducts().find((product) => product.id == productId);

    return new Productdetail(this.product.id, this.product.title);

    /*if (productId === 1) {
      return new Productdetail(1, 'xiaomi note 3');

    } else {
      this.router.navigate(['/home']);
      return undefined;
    }*/

  }
}
